<template>
  <div>
    <common-head title="商品列表" />
    <div class="main">
      <!-- 头部 -->
      <div class="item-nav">
        <div :class="['sale',{active:order_by.sale!==-1}]" @click='bySale'>
          <span >销量</span>
          <div :class="['angle-down',{active:order_by.sale===1}]"></div>
          <div :class="['angle-up',{active:order_by.sale===0}]"></div>
        </div>
        <div :class="['price',{active:order_by.price!==-1}]" @click="byPrice">
          <span>价格</span>
          <div :class="['angle-down',{active:order_by.price===0}]"></div>
          <div :class="['angle-up',{active:order_by.price===1}]"></div>
        </div>
      </div>
      <!-- 列表主体 -->
      <div class="item-main">
        <div
          class="good-item"
          v-for="(item,index) in floors"
          :key="item.id"
          @click="clickItem(index,item.id)"
        >
          <div class="good-pic">
            <img :src="item.pic" alt />
          </div>
          <div class="good-desc">
            <h2>{{item.name}}</h2>
          </div>
          <div class="good-price">
            <span class="now-price">￥{{item.minPrice }}</span>&nbsp;
            <span class="pre-price">￥{{item.originalPrice }}</span>
            <van-icon
              class-prefix="icon"
              class="iconfont"
              name="jiahao"
              size="18"
              color="#C00000"
              id="plus"
            ></van-icon>
          </div>
        </div>
      </div>
    </div>
    <!--  -->
  </div>
</template>

<script>
import CommonHead from '@components/CommonHead'
import { fetchItems } from '@/api'

export default {
  data () {
    return {
      floors: [],
      orderBy: 'ordersDown',
      order_by: {
        sale: 0, // sale 0 默认是 销量降序  1 销量 升序  -1（价格排序）
        price: -1 // price 0 价格升序 1 价格降序  -1  销量排序
      }
    }
  },
  created () {
    this.fetchItem({ orderBy: this.orderBy })
  },
  methods: {
    bySale () {
      this.order_by.price = -1
      if (this.order_by.sale === -1) {
        this.order_by.sale = 0
        // 销量降序
        this.orderBy = 'ordersDown'
      } else if (this.order_by.sale === 0) {
        this.order_by.sale = 1
        this.orderBy = 'ordersUp'
      } else {
        this.order_by.sale = 0
        this.orderBy = 'ordersDown'
      }
      this.fetchItem()
    },
    byPrice () {
      this.order_by.sale = -1
      if (this.order_by.price === -1) {
        this.order_by.price = 0
        this.orderBy = 'priceUp'
      } else if (this.order_by.price === 0) {
        this.order_by.price = 1
        this.orderBy = 'priceDown'
      } else {
        this.order_by.price = 0
        this.orderBy = 'priceUp'
      }
      this.fetchItem()
    },
    fetchItem () {
      const params = {
        orderBy: this.orderBy,
        categoryId: this.$route.params.cateId
      }
      fetchItems(params).then((res) => {
        if (res.data.code === 0) {
          this.floors = res.data.data
        }
      })
    }
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>
.main {
  padding: 46px 0;

  .item-nav {
    width: 100%;
    height: 40px;
    background: #fff;
    display: flex;
    justify-content: space-around;
    position: fixed;
    border-bottom: 1px solid #ccc;
    .sale,
    .price {
      display: flex;
      position: relative;
      &.active{
      color:#f66;
    }
      span {
        font-size: 13px;
        text-align: center;
        line-height: 40px;
      }
      .angle-down {
        width: 0;
        height: 0;
        border: 6px solid transparent;
        border-bottom-color: #aaa;
        position: absolute;
        right: -13px;
        top: 7px;
        &.active {
          border-bottom-color: #f66;
        }
      }
      .angle-up {
        width: 0;
        height: 0;
        border: 6px solid transparent;
        border-top-color: #aaa;
        position: absolute;
        right: -13px;
        top: 21px;
        &.active {
          border-top-color: #f66;
        }
      }
    }
  }
  .item-main {
    margin-top: 41px;
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    box-sizing: border-box;
    .good-item {
      width: 50%;
      box-sizing: border-box;
      .good-pic {
        img {
          width: 100%;
        }
      }
      .good-desc {
        padding: 0 10px;
        h2 {
          width: 100%;
          font-size: 14px;
          height: 40px;
          line-height: 20px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          word-break: break-all;
        }
      }
      .good-price {
        // padding: 0 10px;
        position: relative;
        display: flex;
        align-items: center;
        justify-content: space-around;
        .now-price {
          font-size: 16px;
          color: #f56456;
        }
        .pre-price {
          font-size: 10px;
          color: #999;
          text-decoration: line-through;
          margin-left: -40px;
        }
      }
    }
  }
}
</style>
